package com.yjc.hire.controller;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.yjc.bo.CreateCompanyBO;
import com.yjc.bo.ModifyCompanyInfoBO;
import com.yjc.bo.ReviewCompanyBO;
import com.yjc.hire.VO.CompanyInfoVO;
import com.yjc.hire.VO.CompanySimpleVO;
import com.yjc.hire.VO.UsersVO;
import com.yjc.hire.base.BaseInfoProperties;
import com.yjc.hire.exceptions.GraceException;
import com.yjc.hire.feign.UserMicroServiceFeign;
import com.yjc.hire.grace.result.GraceJSONResult;
import com.yjc.hire.grace.result.ResponseStatusEnum;
import com.yjc.hire.service.CompanyService;
import com.yjc.pojo.Company;
import com.yjc.pojo.CompanyPhoto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * </p>
 *
 * @author jcyin
 * @since 2025/7/23
 * </p>
 */

@RestController
@RequestMapping("company")
public class CompanyController extends BaseInfoProperties {
    @Autowired
    private CompanyService companyService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserMicroServiceFeign userMicroServiceFeign;

    @PostMapping("getByFullName")
    public GraceJSONResult getByFullName(String fullName){
        if (StringUtils.isBlank(fullName)){
            return GraceJSONResult.error();
        }
        Company company = companyService.findCompanyByFullName(fullName);
        if (company == null) return GraceJSONResult.ok(null);
        CompanySimpleVO vo = new CompanySimpleVO();
        BeanUtils.copyProperties(company,vo);
        return GraceJSONResult.ok(vo);
    }
    /*8
    创建新企业或者重新审核
     */
    @PostMapping("createNewCompany")
    public GraceJSONResult createNewCompany(@RequestBody CreateCompanyBO companyBO){
        String companyId = companyBO.getCompanyId();
        String doCompanyId = "";
        if (StringUtils.isBlank(companyId)){
            //为空创建
            doCompanyId = companyService.createNewCompany(companyBO);
        }
        else{
            //不为空则修改
            doCompanyId = companyService.resetCompany(companyBO);
        }
        return GraceJSONResult.ok(doCompanyId);
    }
    @PostMapping("getInfo")
    public GraceJSONResult getInfo(String companyId, boolean withHrCounts){
        CompanySimpleVO company = getCompany(companyId);
        if (withHrCounts && company != null){
            //远程调用查询总hr绑定数
            GraceJSONResult graceJSONResult = userMicroServiceFeign.getCountByCompanyId(companyId);
            Object data = graceJSONResult.getData();
            Long hrCount = (Long) data;
            company.setHrCounts(hrCount);
        }
        return GraceJSONResult.ok(company);
    }

    private CompanySimpleVO getCompany(String companyId){
        if (StringUtils.isBlank(companyId)){
            return null;
        }
        String key = REDIS_COMPANY_BASE_INFO + ":" + companyId;
        String companyJson = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isBlank(companyJson)){
            //查询数据库
            Company company = companyService.getCompanyById(companyId);
            if (company == null) return null;

            CompanySimpleVO simpleVO = new CompanySimpleVO();
            BeanUtils.copyProperties(company,simpleVO);
            stringRedisTemplate.opsForValue()
                    .set(key,new Gson().toJson(simpleVO),5 * 60);
            return simpleVO;
        }else {
            return new Gson().fromJson(companyJson, CompanySimpleVO.class);
        }
    }

    @PostMapping()
    public GraceJSONResult goReviewCompany(@RequestBody ReviewCompanyBO reviewCompanyBO){
        //远程调用绑定hr企业id
        GraceJSONResult result = userMicroServiceFeign.bindingHRToCompany(reviewCompanyBO.getHrUserId(), reviewCompanyBO.getCompanyId(), reviewCompanyBO.getRealName());
        String heMobile = result.getData().toString();
        //保存审核信息
        reviewCompanyBO.setHrMobile(heMobile);
        companyService.commitReviewCompanyInfo(reviewCompanyBO);
        return GraceJSONResult.ok();
    }

    @PostMapping("information")
    public GraceJSONResult information(String hrUserId){
        UsersVO infoVO = getHRInfoVO(hrUserId);
        CompanySimpleVO company = getCompany(infoVO.getHrInWhichCompanyId());
        return GraceJSONResult.ok(company);
    }
    private UsersVO getHRInfoVO(String hrUserId){
        GraceJSONResult result = userMicroServiceFeign.get(hrUserId);
        Object data = result.getData();
        String json = new Gson().toJson(data);
        return new Gson().fromJson(json, UsersVO.class);
    }

    @PostMapping("info")
    public GraceJSONResult info(String userId){
        UsersVO infoVO = getHRInfoVO(userId);
        CompanySimpleVO company = getCompany(infoVO.getHrInWhichCompanyId());
        return GraceJSONResult.ok(company);
    }

    @PostMapping("moreInfo")
    public GraceJSONResult moreInfo(String companyId){
        CompanyInfoVO companyInfoVO = getMoreCompanyInfo(companyId);
        return GraceJSONResult.ok(companyInfoVO);
    }
    private CompanyInfoVO getMoreCompanyInfo(String companyId){
        if (StringUtils.isBlank(companyId))return null;
        String key = REDIS_COMPANY_MORE_INFO + ":" + companyId;
        String companyJson = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(companyJson)){
            Company company = companyService.getCompanyById(companyId);
            if (company == null){
                return null;
            }
            CompanyInfoVO companyInfoVO = new CompanyInfoVO();
            BeanUtils.copyProperties(company, companyInfoVO);
            stringRedisTemplate.opsForValue().set(key,new Gson().toJson(companyInfoVO), 60);
            return companyInfoVO;
        }else {
            return new Gson().fromJson(companyJson, CompanyInfoVO.class);
        }
    }

    @PostMapping("modify")
    public GraceJSONResult modifyCompany(@RequestBody ModifyCompanyInfoBO companyInfoBO){
        checkUserCompany(companyInfoBO.getCurrentUserId(), companyInfoBO.getCompanyId());
        //修改信息
        companyService.modifyCompanyInfo(companyInfoBO);
        //企业相册信息的保存
        if (StringUtils.isNoneBlank(companyInfoBO.getPhotos())){
            companyService.saveCompanyPhoto(companyInfoBO);
        }
        return GraceJSONResult.ok();
    }

    /**
     * 校验企业下的hr
     * @param userId
     * @param companyId
     */
    private void checkUserCompany(String userId,String companyId){
        if (StringUtils.isBlank(userId)){
            GraceException.disPlay(ResponseStatusEnum.COMPANY_INFO_UPDATED_NO_AUTH_ERROR);
        }
        UsersVO usersVO = getHRInfoVO(userId);
        if (!usersVO.getHrInWhichCompanyId().equalsIgnoreCase(companyId)){
            GraceException.disPlay(ResponseStatusEnum.COMPANY_INFO_UPDATED_NO_AUTH_ERROR);
        }
    }

    @PostMapping("getPhotos")
    public GraceJSONResult getPhotos(String companyId){
        CompanyPhoto companyPhotos = companyService.getPhotoByCompanyId(companyId);
        return GraceJSONResult.ok(companyPhotos);
    }

}
